package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.RequireLogin;
import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.qurey.StrategyCommentQuery;
import cn.wolfcode.wolf2w.qurey.StrategyQuery;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.service.impl.DestinationServiceImpl;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.jar.JarEntry;

@RestController
@RequestMapping("/strategies")
public class StrategyController {
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyThemeService strategyThemeService;
    @Autowired
    private IStrategyCommentService strategyCommentService;
    @Autowired
    private IStrategyStatisVORedisService redisService;


    @GetMapping("/detail")
    private Object detail(String id){

        redisService.viewnumIncrease(id,1);
        return JsonResult.success(strategyService.get(id));
    }


    @GetMapping("/query")
    private Object query(StrategyQuery qo){
        return JsonResult.success(strategyService.query(qo));
    }

    @GetMapping("/themes")
    private Object themes(){
        return JsonResult.success(strategyThemeService.list());
    }


    @PostMapping("/addComment")
    @RequireLogin
    private Object addComment(StrategyComment comment, @UserParam UserInfo userInfo){
       //设置用户相关信息
        //约定:
        //1.源对象(Ctrl+c的位置) 参数2:目标对象(Ctrl+V的位置)
        //2.源对象属性名必须跟目的对象属性名一致是才可以属性值赋值
        //底层:javabean的内省机制
        BeanUtils.copyProperties(userInfo, comment);
        comment.setUserId(userInfo.getId());
        strategyCommentService.save(comment);
        //评论计数
        redisService.increaseReplynum(comment.getStrategyId(),1 );
        return JsonResult.success();
    }

    @GetMapping("/comments")
    private Object comments(StrategyCommentQuery qo){
        return JsonResult.success(strategyCommentService.query(qo));
    }

    @PostMapping("/commentThumb")
    @RequireLogin
    private Object commentThumb(String cid,@UserParam UserInfo userInfo){
        strategyCommentService.commentThumb(cid,userInfo.getId());
        return JsonResult.success();
    }
    @GetMapping("/statisVo")
    private Object statisVo(String sid){
        return JsonResult.success(redisService.getStrategyStatisVo(sid));
    }

    @PostMapping("/favor")
    @RequireLogin
    private Object favor(String sid,@UserParam UserInfo userInfo){
        //攻略收藏
        Boolean ret=redisService.favor(sid,userInfo.getId());
        return JsonResult.success(ret);
    }
    @PostMapping("/strategyThumbup")
    @RequireLogin
    private Object strategyThumbup(String sid,@UserParam UserInfo userInfo){
        //点赞
        Boolean ret=redisService.strategyThumbup(sid,userInfo.getId());
        return JsonResult.success(ret);
    }


}
